# encoding: utf-8
# 获取工程信息时添加缓存
from app.extensions import db,log
from app.modules.project.models import ProjectInfo
from app.modules.projectPlan.models import ProjectPlanInfo
from app import cache


def getProByParam(plan_id = None, plan_ids = None, trunk_name = None, project_status = None, project_name = None,
                  isHideMergePro = None, isOnBeetle = None, not_contain_package_type = None, package_type = None,
                  withCache = False):
    log.info("============== getProByParam ==============")
    _list = []
    if withCache:
        _list = getProListByParamWithCache(plan_id = plan_id, plan_ids = plan_ids, trunk_name = trunk_name,
                                           project_status = project_status, project_name = project_name,
                                           isHideMergePro = isHideMergePro, isOnBeetle = isOnBeetle,
                                           not_contain_package_type = not_contain_package_type,
                                           package_type = package_type)
    else:
        _list = getProListByParam(plan_id = plan_id, plan_ids = plan_ids, trunk_name = trunk_name,
                                  project_status = project_status, project_name = project_name,
                                  isHideMergePro = isHideMergePro, isOnBeetle = isOnBeetle,
                                  not_contain_package_type = not_contain_package_type, package_type = package_type)

    if len(_list) == 0:
        return None
    else:
        return _list[0]


@cache.memoize()
def getProListByParamWithCache(plan_id = None, plan_ids = None, trunk_name = None, project_status = None,
                               project_name = None, isHideMergePro = None, isOnBeetle = None,
                               not_contain_package_type = None, package_type = None):
    log.info("============== getProListByParamWithCache ==============")
    return getProListByParam(plan_id = plan_id, plan_ids = plan_ids, trunk_name = trunk_name,
                             project_status = project_status, project_name = project_name,
                             isHideMergePro = isHideMergePro, isOnBeetle = isOnBeetle,
                             not_contain_package_type = not_contain_package_type, package_type = package_type)


def getProListByParam(plan_id = None, plan_ids = None, trunk_name = None, project_status = None, project_name = None,
                      isHideMergePro = None, isOnBeetle = None, not_contain_package_type = None, package_type = None):
    log.info("============== getProListByParam ==============")
    _query = db.session.query(ProjectInfo, ProjectPlanInfo.plan_name).filter(
        ProjectInfo.plan_id == ProjectPlanInfo.plan_id)
    if project_name is not None:
        _query = _query.filter(ProjectInfo.project_name == project_name)
    if plan_id is not None:
        _query = _query.filter(ProjectInfo.plan_id == plan_id)
    if plan_ids is not None:
        _query = _query.filter(ProjectInfo.plan_id.in_(plan_ids))
    if trunk_name is not None:
        _query = _query.filter(ProjectInfo.trunk_name == trunk_name)
    if project_status is not None:
        _query = _query.filter(ProjectInfo.project_status == project_status)
    if isHideMergePro:
        _query = _query.filter(ProjectInfo.project_status != 'PRO_MERGED')
    if isOnBeetle is not None:
        _query = _query.filter(ProjectInfo.isOnBeetle == isOnBeetle)
    if not_contain_package_type is not None:
        _query = _query.filter(ProjectInfo.package_type != not_contain_package_type)
    if package_type is not None:
        _query = _query.filter(ProjectInfo.package_type == package_type)

    _list = []
    for projectInfo, plan_name in _query.order_by(ProjectInfo.project_name.asc()).all():
        projectInfo.plan_name = plan_name
        _list.append(projectInfo)
    return _list
